package com.flea.service;

import com.flea.exception.ServiceException;
import com.flea.model.Product;
import com.flea.model.User;
import com.flea.repository.jdbc.ProductRepository;
import com.flea.repository.jdbc.UserRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author hc24
 * @date 2021.1.20 13:00
 */
@Service
public class UserService {

    @Resource
    private UserRepository userRepository;

    @Resource
    private ProductRepository productRepository;

    public User login(String login,String pwd){
        User user=userRepository.findByLoginAndPwd(login,pwd);
        if(user==null){
            throw new ServiceException("用户名或密码错误");
        }
        return user;
    }

    public void register(User user) {
        //重名监测
        if (userRepository.countByLoginOrNickname(user.getLogin(), user.getNickname()) > 0) {
            throw new ServiceException("登录名或昵称已存在");
        }
        userRepository.save(user);
    }

    public Page<Product> findProduct(Integer userId, String key, Integer minPrice, Pageable pageable) {
        return productRepository.findByCondition(userId, key, minPrice, pageable);
    }

    public long countAllProduct(Integer userId) {
        return productRepository.countByUserId(userId);
    }


    public void test() {
        // Predicate predicate =

        //userRepository.findAll(predicate);

    }
}
